package web.demo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import web.demo.entity.Food;
import web.demo.entity.FoodTable;

public class FoodTableServlet extends BaseServlet {
	
	public Object test(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		return null;
	}
	//显示餐桌信息
	public Object foodTableList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<FoodTable> foodTableList=foodTableService.getAll();
		request.setAttribute("foodTableList",foodTableList);
		return request.getRequestDispatcher("sys/board/foodTableList.jsp");
	}
	//添加餐桌
	public Object saveFoodTable(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//得到餐桌名称
		String foodTableName=null;
		if(request.getParameter("foodTableName")!=null && !"".equals(request.getParameter("foodTableName").trim())){
			foodTableName=request.getParameter("foodTableName");
		}else{
			return "error/error.jsp";
		}
		FoodTable foodTable=new FoodTable();
		foodTable.setName(foodTableName);
		foodTableService.addFoodTable(foodTable);
		return request.getRequestDispatcher("FoodTableServlet?method=foodTableList");
	}
	//预定餐桌
	public Object prepareFoodTable(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int foodTableId=Integer.parseInt(request.getParameter("foodTableId"));
		FoodTable foodTable=foodTableService.getById(foodTableId);
		foodTable.setStatus(1);
		foodTable.setPrepare_time(new Date());
		//更新数据库
		foodTableService.update(foodTable);
		return request.getRequestDispatcher("FoodTableServlet?method=foodTableList");
	}
	//退订餐桌
	public Object backFoodTable(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int foodTableId=Integer.parseInt(request.getParameter("foodTableId"));
		FoodTable foodTable=foodTableService.getById(foodTableId);
		foodTable.setStatus(0);
		foodTable.setPrepare_time(null);
		//更新数据库
		foodTableService.update(foodTable);
		return request.getRequestDispatcher("FoodTableServlet?method=foodTableList");
	}
	//查询餐桌
	public Object search(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String selectCondition="";
		if(request.getParameter("selectCondition")!=null){
			selectCondition=new String(request.getParameter("selectCondition").getBytes("ISO-8859-1"),"utf-8");
		}
		List<FoodTable> foodTableList=foodTableService.select(selectCondition);
		request.setAttribute("foodTableList",foodTableList);
		
		return request.getRequestDispatcher("sys/board/foodTableList.jsp");
	}
	
	
}
